<?php
// // 建立数据库连接
// // 更新房源
$jsonData = file_get_contents('php://input');
$data = json_decode($jsonData, true);
$roomId = $data['roomId'] ?? null;
$roomNumber = $data['roomNumber'] ?? null;
$roomType = $data['roomType'] ?? null;
$roomStatus = $data['roomStatus'] ?? null;
$price = $data['price'] ?? null;
$capacity = $data['capacity'] ?? null;
$fileInput = $data['fileInput'] ?? null;
$roomService = $data['roomService'] ?? null;
$roomDescription = $data['roomDescription'] ?? null;
$roomIntroduction = $data['roomIntroduction'] ?? null;

// 打印变量的值
echo 'roomId: ' . $roomId . '<br>';
echo 'roomNumber: ' . $roomNumber . '<br>';
echo 'roomType: ' . $roomType . '<br>';
echo 'roomStatus: ' . $roomStatus . '<br>';
echo 'price: ' . $price . '<br>';
echo 'capacity: ' . $capacity . '<br>';
echo 'fileInput: ' . $fileInput . '<br>';
echo 'roomService: ' . $roomService . '<br>';
echo 'roomDescription: ' . $roomDescription . '<br>';
echo 'roomIntroduction: ' . $roomIntroduction . '<br>';


require_once 'databaseConnection.php';
// 获取通过 POST 请求发送的数据
// $roomNumber = $_POST['roomNumber'] ?? null;
// $roomType = $_POST['roomType'] ?? null;
// $roomStatus = $_POST['roomStatus'] ?? null;
// $price = $_POST['price'] ?? null;
// $capacity = $_POST['capacity'] ?? null;
// $fileInput = $_POST['fileInput'] ?? null;
// $roomService = $_POST['roomService'] ?? null;
// $roomDescription = $_POST['roomDescription'] ?? null;
// $roomIntroduction = $_POST['roomIntroduction'] ?? null;
// $roomId = $_POST['rooms_id'] ?? null;
try {
    // 检查 roomId 是否存在
    if ($roomId !== null) {
        $dbConn = connectToDatabase();

        $stmt = $dbConn->prepare('SELECT * FROM rooms WHERE rooms_id = :roomId');
        $stmt->bindParam(':roomId', $roomId);
        $stmt->execute();

        // 检查 roomId 对应的行是否存在
        if ($stmt->rowCount() > 0) {
            $row = $stmt->fetch(PDO::FETCH_ASSOC);

            // 根据 POST 数据更新字段，如果字段为空，则保留原值
            $roomNumber = $roomNumber ?? $row['roomNumber'];
            $roomType = $roomType ?? $row['roomType'];
            $roomStatus = $roomStatus ?? $row['roomStatus'];
            $price = $price ?? $row['price'];
            $capacity = $capacity ?? $row['capacity'];
            $fileInput = $fileInput ?? $row['fileInput'];
            $roomService = $roomService ?? $row['roomService'];
            $roomDescription = $roomDescription ?? $row['roomDescription'];
            $roomIntroduction = $roomIntroduction ?? $row['roomIntroduction'];

            $stmt = $dbConn->prepare('UPDATE rooms SET roomNumber = :roomNumber, roomType = :roomType, roomStatus = :roomStatus, price = :price, capacity = :capacity, fileInput = :fileInput, roomService = :roomService, roomDescription = :roomDescription, roomIntroduction = :roomIntroduction WHERE rooms_id = :roomId');

            $stmt->bindParam(':roomId', $roomId);
            $stmt->bindParam(':roomNumber', $roomNumber);
            $stmt->bindParam(':roomType', $roomType);
            $stmt->bindParam(':roomStatus', $roomStatus);
            $stmt->bindParam(':price', $price);
            $stmt->bindParam(':capacity', $capacity);
            $stmt->bindParam(':fileInput', $fileInput);
            $stmt->bindParam(':roomService', $roomService);
            $stmt->bindParam(':roomDescription', $roomDescription);
            $stmt->bindParam(':roomIntroduction', $roomIntroduction);

            echo '查询到的记录数：' . $stmt->rowCount();
            $stmt->execute();

            echo '更新成功';
        } else {
            echo 'roomId 对应的行不存在';
        }

        $dbConn = null;
    } else {
        echo 'roomId 不存在';
    }
} catch (PDOException $e) {
    echo 'PDO 异常信息：' . $e->getMessage();
    echo '数据库错误：' . $e->getMessage();
}
